from os import path, listdir
from setting import DB_Floder_Path
import queue
import socket
import binascii
import sqlite3

from sensor.sensor import *


class SingletonDecorator(object):
    _instance = None

    def __init__(self, cls):
        self._cls = cls

    def __call__(self, *args, **kwargs):
        if self._instance is None:
            self._instance = self._cls(*args, **kwargs)


def singleton(cls):
    _instance = {}

    def inner():
        if cls not in _instance:
            _instance[cls] = cls()
        return _instance[cls]

    return inner


@singleton
class Skt:
    def __init__(self):
        self.conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        recv_addr = ("", 1399)
        self.conn.bind(recv_addr)
        # 设置允许广播
        self.conn.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

    def send_broadcast_for_sensor(self):
        ip_address = socket.gethostbyname(socket.gethostname())
        all_addr = ("255.255.255.255", 9250)
        send_data = ("WIT" + ip_address + "\r\n").encode()
        self.conn.sendto(send_data, all_addr)

    def get_sensor_raw_data(self):
        recv_data = self.conn.recvfrom(1024)
        data_str = binascii.b2a_hex(recv_data[0]).decode('utf-8')
        return data_str

    def get_sensor_data(self):
        raw_data = self.get_sensor_raw_data()
        int_data = sensor_raw_to_int(raw_data)
        return tread_sensor_values(int_data)


@singleton
class SQL_Conn:
    def __init__(self):
        self.conn = sqlite3.connect('./m_data.db', check_same_thread=False)

    def get_cur(self):
        return self.conn.cursor()


@singleton
class PublicData:
    def __init__(self):
        self.sensor_code_list = []
        self.sensor_dic = dict()
        self.location_list = ["A端", "B端", "底座A侧", "底座B侧"]
        self.agree_list = ["x", "y"]
        self.product = None
        self.skt = Skt()
        self.tb_sensor_col_list = ["sensor_id", "location", "position", "angle_x", "angle_y", "angle_z", "acc_x",
                                   "acc_y", "acc_z", "acc_x", "bat"]
        self.sensor_data_col = ["sensor_code", "d_date", "d_time", "acc_x", "acc_y", "acc_z", "gyro_x", "gyro_y",
                                "gyro_z", "mag_x", "mag_y", "mag_z", "angle_x", "angle_y", "angle_z", "temp", "bat",
                                "rss",
                                "sensor_id"]
        self.calculate_col = ["sensor_id", "acc_x", "acc_y", "acc_z", "gyro_x", "gyro_y", "gyro_z",
                              "mag_x", "mag_y", "mag_z", "angle_x", "angle_y", "angle_z", "temp", "bat", "rss"]

        self.queue_sensor_data = queue.Queue(100)
        self.data_read_signal = True
        self.data_save_signal = False
        self.df_start_mean = None
        self.temp_sensor_data_list = []
        self.has_start_record = False
        self.agree_data_list_dict = dict()  # 角度值的列表，用于
        self.acc_data_list_dict = dict()
        self.bend_agree_list = []
        self.agree_max = 0
        self.bend_data_dict = dict()


